package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.MessageNotice;
import com.myzl.coal.mapper.MessageNoticeMapper;
import com.myzl.coal.service.IMessageNoticeService;
import com.myzl.common.enums.NoticeStatusEnum;
import com.myzl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 用户消息通知Service业务层处理
 *
 * @author myzl
 * @date 2023-01-11
 */
@Service
public class MessageNoticeServiceImpl extends ServiceImpl<MessageNoticeMapper, MessageNotice> implements IMessageNoticeService {
    @Autowired
    private MessageNoticeMapper messageNoticeMapper;

    /**
     * 查询用户消息通知
     *
     * @param id 用户消息通知主键
     * @return 用户消息通知
     */
    @Override
    public MessageNotice selectMessageNoticeById(Long id) {
        return messageNoticeMapper.selectMessageNoticeById(id);
    }

    /**
     * 查询用户消息通知列表
     *
     * @param messageNotice 用户消息通知
     * @return 用户消息通知
     */
    @Override
    public List<MessageNotice> selectMessageNoticeList(MessageNotice messageNotice) {
        return messageNoticeMapper.selectMessageNoticeList(messageNotice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MessageNotice> getMessageNoticeList(MessageNotice messageNotice) {
        List<MessageNotice> messageNoticeList = messageNoticeMapper.selectMessageNoticeList(messageNotice);

        messageNoticeList.forEach(e -> {
            e.setNoticeStatus(NoticeStatusEnum.YES.getCode());
            e.setNoticeTime(new Date());
        });
        this.updateBatchById(messageNoticeList);
        return messageNoticeList;
    }

    /**
     * 新增用户消息通知
     *
     * @param messageNotice 用户消息通知
     * @return 结果
     */
    @Override
    public int insertMessageNotice(MessageNotice messageNotice) {
        messageNotice.setCreateTime(DateUtils.getNowDate());
        return messageNoticeMapper.insertMessageNotice(messageNotice);
    }

    /**
     * 修改用户消息通知
     *
     * @param messageNotice 用户消息通知
     * @return 结果
     */
    @Override
    public int updateMessageNotice(MessageNotice messageNotice) {
        messageNotice.setUpdateTime(DateUtils.getNowDate());
        return messageNoticeMapper.updateMessageNotice(messageNotice);
    }

    /**
     * 批量删除用户消息通知
     *
     * @param ids 需要删除的用户消息通知主键
     * @return 结果
     */
    @Override
    public int deleteMessageNoticeByIds(Long[] ids) {
        return messageNoticeMapper.deleteMessageNoticeByIds(ids);
    }

    /**
     * 删除用户消息通知信息
     *
     * @param id 用户消息通知主键
     * @return 结果
     */
    @Override
    public int deleteMessageNoticeById(Long id) {
        return messageNoticeMapper.deleteMessageNoticeById(id);
    }

    @Override
    public int clean(Long userId) {
        MessageNotice params = new MessageNotice();
        params.setUserId(userId);
        List<MessageNotice> messageNoticeList = messageNoticeMapper.selectMessageNoticeList(params);
        if (CollectionUtils.isEmpty(messageNoticeList)) {
            return 1;
        }
        Long[] ids = messageNoticeList.stream().map(MessageNotice::getId).toArray(Long[]::new);
        int i = messageNoticeMapper.deleteMessageNoticeByIds(ids);
        return i;
    }
}
